#include "../HUICCirclePanelNoAni.h"
#include "../../include/StdAfx.h"
#include <QDebug>

HUICCirclePanelNoAni::HUICCirclePanelNoAni(QWidget *parent)
    : QWidget(parent),m_nPositionX(0),m_nPositionY(0),m_enumCurrentState(BP_INVALID_REGION)
    ,m_PressAfter(0,0),m_bMousePress(false)
{
    this->setObjectName("uic_wnd_circelPanelNoAni");
    this->setMouseTracking(true);
    this->setFixedSize(BUTTON_SIZE,BUTTON_SIZE);

    QString backgroundPath = g_strAppPath + g_strImageRelationPath + "HIDI_CIRCLEPANELNOANI_BG.png";//":/Resource/ptz_bg.png";

    m_pixBackground = QPixmap(backgroundPath);
    //this->setStyleSheet("background-color:red");
}

HUICCirclePanelNoAni::~HUICCirclePanelNoAni()
{

}

int HUICCirclePanelNoAni::PerssPosition(QPoint pos)
{
    int pos_x = pos.x();
    int pos_y = pos.y();

   // int offset_x = -10;
    //int offset_y = -10;

    int offset_x = 0;
    int offset_y = 0;

//    if(m_enumCurrentState == 0 || m_enumCurrentState == BP_CENTER)
//    {
//        offset_x = 0;
//        offset_y = 0;
//    }else if(m_enumCurrentState == BP_UP || m_enumCurrentState == BP_DOWN)
//    {
//        if(m_enumCurrentState == BP_UP)
//        {
//            offset_x = 0;
//            offset_y = -BUTTON_PRESS_OFFSET;
//        }else
//        {
//            offset_x = 0;
//            offset_y = BUTTON_PRESS_OFFSET;
//        }
//    }else if(m_enumCurrentState == BP_LEFT || m_enumCurrentState == BP_RIGHT)
//    {
//        if(m_enumCurrentState == BP_LEFT)
//        {
//            offset_x = -BUTTON_PRESS_OFFSET;
//            offset_y = 0;
//        }else
//        {
//            offset_x = BUTTON_PRESS_OFFSET;
//            offset_y = 0;
//        }
//    }else if(m_enumCurrentState == BP_LEFT_UP || m_enumCurrentState == BP_RIGHT_DOWN)
//    {
//        if(m_enumCurrentState == BP_LEFT_UP)
//        {
//            offset_x = -BUTTON_PRESS_OFFSET/1.414;
//            offset_y = offset_x;
//        }else
//        {
//            offset_x = BUTTON_PRESS_OFFSET/1.414;
//            offset_y = offset_x;
//        }
//    }else if(m_enumCurrentState == BP_RIGHT_UP || m_enumCurrentState == BP_LEFT_DOWN)
//    {
//        if(m_enumCurrentState == BP_RIGHT_UP)
//        {
//            offset_x = BUTTON_PRESS_OFFSET/1.414;
//            offset_y = -offset_x;
//        }else
//        {
//            offset_x = -BUTTON_PRESS_OFFSET/1.414;
//            offset_y = -offset_x;
//        }
//    }

    if((pos_x - (BUTTON_SIZE/2+offset_x))*(pos_x - (BUTTON_SIZE/2+offset_x)) + (pos_y - (BUTTON_SIZE/2+offset_y))*(pos_y - (BUTTON_SIZE/2+offset_y)) <= CENTER_DISK_R*CENTER_DISK_R)
    {
        return BP_CENTER;
    }else if((pos_x - (UP_CENTER.x()+offset_x))*(pos_x - (UP_CENTER.x()+offset_x)) + (pos_y - (UP_CENTER.y()+offset_y))*(pos_y - (UP_CENTER.y()+offset_y)) <= UP_CENTER_R*UP_CENTER_R)
    {
        return BP_UP;
    }else if((pos_x - (DOWN_CENTER.x()+offset_x))*(pos_x - (DOWN_CENTER.x()+offset_x)) + (pos_y - (DOWN_CENTER.y()+offset_y))*(pos_y - (DOWN_CENTER.y()+offset_y)) <= DOWN_CENTER_R*DOWN_CENTER_R)
    {
        return BP_DOWN;
    }else if((pos_x - (LEFT_CENTER.x()+offset_x))*(pos_x - (LEFT_CENTER.x()+offset_x)) + (pos_y - (LEFT_CENTER.y()+offset_y))*(pos_y - (LEFT_CENTER.y()+offset_y)) <= LEFT_CENTER_R*LEFT_CENTER_R)
    {
        return BP_LEFT;
    }else if((pos_x - (RIGHT_CENTER.x()+offset_x))*(pos_x - (RIGHT_CENTER.x()+offset_x)) + (pos_y - (RIGHT_CENTER.y()+offset_y))*(pos_y - (RIGHT_CENTER.y()+offset_y)) <= RIGHT_CENTER_R*RIGHT_CENTER_R)
    {
        return BP_RIGHT;
    }
    else if((pos_x - (LEFT_UP_CENTER.x()+offset_x))*(pos_x - (LEFT_UP_CENTER.x()+offset_x)) + (pos_y - (LEFT_UP_CENTER.y()+offset_y))*(pos_y - (LEFT_UP_CENTER.y()+offset_y)) <= LEFT_UP_R*LEFT_UP_R)
    {
        return BP_LEFT_UP;
    }else if((pos_x - (RIGHT_UP_CENTER.x()+offset_x))*(pos_x - (RIGHT_UP_CENTER.x()+offset_x)) + (pos_y - (RIGHT_UP_CENTER.y()+offset_y))*(pos_y - (RIGHT_UP_CENTER.y()+offset_y)) <= RIGHT_UP_R*RIGHT_UP_R)
    {
        return BP_RIGHT_UP;
    }else if((pos_x - (LEFT_DOWN_CENTER.x()+offset_x))*(pos_x - (LEFT_DOWN_CENTER.x()+offset_x)) + (pos_y - (LEFT_DOWN_CENTER.y()+offset_y))*(pos_y - (LEFT_DOWN_CENTER.y()+offset_y)) <= LEFT_DOWN_R*LEFT_DOWN_R)
    {
        return BP_LEFT_DOWN;
    }else if((pos_x - (RIGHT_DOWN_CENTER.x()+offset_x))*(pos_x - (RIGHT_DOWN_CENTER.x()+offset_x)) + (pos_y - (RIGHT_DOWN_CENTER.y()+offset_y))*(pos_y - (RIGHT_DOWN_CENTER.y()+offset_y)) <= RIGHT_DOWN_R*RIGHT_DOWN_R)
    {
        return BP_RIGHT_DOWN;
    }else
    {
        return BP_INVALID_REGION;
    }

}

void HUICCirclePanelNoAni::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    QPainter painter(this);
    painter.translate(-BG_LEFT_OFFSET,-BG_UP_OFFSET);
    painter.translate(m_nPositionX,m_nPositionY);
    painter.drawPixmap(0, 0, m_pixBackground);

    QWidget::paintEvent(event);
}

void HUICCirclePanelNoAni::mouseMoveEvent(QMouseEvent *event)
{
    m_PressCurrent = event->pos();
    if(m_enumCurrentState == 0)
    {
        switch (PerssPosition(event->pos())) {
        case BP_CENTER:
            m_nPositionX = 0;
            m_nPositionY = -BUTTON_OFFSET;
            break;
        case BP_UP:
            m_nPositionX = -BUTTON_OFFSET;
            m_nPositionY = 0;
            break;
        case BP_DOWN:
            m_nPositionX = -(BUTTON_OFFSET+BUTTON_OFFSET);
            m_nPositionY = 0;
            break;
        case BP_LEFT:
            m_nPositionX = -(BUTTON_OFFSET+BUTTON_OFFSET+BUTTON_OFFSET);
            m_nPositionY = 0;
            break;
        case BP_RIGHT:
            m_nPositionX = -(BUTTON_OFFSET+BUTTON_OFFSET+BUTTON_OFFSET+BUTTON_OFFSET);
            m_nPositionY = 0;
            break;
        case BP_LEFT_UP:
            m_nPositionX = -BUTTON_OFFSET;
            m_nPositionY = -BUTTON_OFFSET;
            break;
        case BP_RIGHT_UP:
            m_nPositionX = -(BUTTON_OFFSET+BUTTON_OFFSET);
            m_nPositionY = -BUTTON_OFFSET;
            break;
        case BP_LEFT_DOWN:
            m_nPositionX = -(BUTTON_OFFSET+BUTTON_OFFSET+BUTTON_OFFSET+BUTTON_OFFSET);
            m_nPositionY = -BUTTON_OFFSET;
            break;
        case BP_RIGHT_DOWN:
            m_nPositionX = -(BUTTON_OFFSET+BUTTON_OFFSET+BUTTON_OFFSET);
            m_nPositionY = -BUTTON_OFFSET;
            break;
        default:
            m_nPositionX = 0;
            m_nPositionY = 0;
            break;
        }
        update();
    }
    QWidget::mouseMoveEvent(event);
}

void HUICCirclePanelNoAni::mousePressEvent(QMouseEvent *event)
{
	if (event->button() & Qt::LeftButton)
	{
		m_bMousePress = true;
		m_PressCurrent = event->pos();
		m_PressAfter = event->pos();

		if (PerssPosition(m_PressAfter) == BP_CENTER)
		{
            m_nPositionX = 0;
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_CENTER;
			emit sigClick(BP_CENTER, BTN_DOWN);
		}
		switch (PerssPosition(m_PressAfter)) {
		case BP_UP:
            m_nPositionX = -BUTTON_OFFSET ;
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_UP;
			emit sigClick(BP_UP, BTN_DOWN);
			break;
		case BP_DOWN:
            m_nPositionX = -(BUTTON_OFFSET + BUTTON_OFFSET);;
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_DOWN;
			emit sigClick(BP_DOWN, BTN_DOWN);
			break;
		case BP_LEFT:
            m_nPositionX = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_LEFT;
			emit sigClick(BP_LEFT, BTN_DOWN);
			break;
		case BP_RIGHT:
            m_nPositionX = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_RIGHT;
			emit sigClick(BP_RIGHT, BTN_DOWN);
			break;
		case BP_LEFT_UP:
            m_nPositionX = -BUTTON_OFFSET;
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_LEFT_UP;
			emit sigClick(BP_LEFT_UP, BTN_DOWN);
			break;
		case BP_RIGHT_UP:
            m_nPositionX = -(BUTTON_OFFSET + BUTTON_OFFSET);
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_RIGHT_UP;
			emit sigClick(BP_RIGHT_UP, BTN_DOWN);
			break;
		case BP_LEFT_DOWN:
            m_nPositionX = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_LEFT_DOWN;
			emit sigClick(BP_LEFT_DOWN, BTN_DOWN);
			break;
		case BP_RIGHT_DOWN:
            m_nPositionX = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
            m_nPositionY = -(BUTTON_OFFSET + BUTTON_OFFSET + BUTTON_OFFSET);
			m_enumCurrentState = BP_LEFT_DOWN;
			emit sigClick(BP_RIGHT_DOWN, BTN_DOWN);
			break;
		}
		update();
		event->accept();
	}
}

void HUICCirclePanelNoAni::mouseReleaseEvent(QMouseEvent *event)
{
	if (m_bMousePress)
	{
        m_nPositionX = 0;
		m_nPositionY = 0;
		emit sigClick(m_enumCurrentState, BTN_UP);
		m_enumCurrentState = BP_INVALID_REGION;
		update();
	}
	m_bMousePress = false;
	event->accept();
}
